package com.yf.repo.modules.repo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.base.api.exception.ServiceException;
import com.yf.repo.modules.repo.entity.RepoDepart;
import com.yf.repo.modules.repo.mapper.RepoDepartMapper;
import com.yf.repo.modules.repo.service.RepoDepartService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* <p>
* 题库部门业务实现类
* </p>
*
* @author 聪明笨狗
* @since 2022-02-11 10:42
*/
@Service
public class RepoDepartServiceImpl extends ServiceImpl<RepoDepartMapper, RepoDepart> implements RepoDepartService {

    @Override
    public void saveAll(String repoId, List<String> departs) {

        // 先删除
        QueryWrapper<RepoDepart> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RepoDepart::getRepoId, repoId);
        this.remove(wrapper);

        // 再增加
        if(CollectionUtils.isEmpty(departs)){
            throw new ServiceException("请至少选择选择一个部门！！");
        }
        List<RepoDepart> list = new ArrayList<>();

        for(String code: departs){
            RepoDepart depart = new RepoDepart();
            depart.setDeptCode(code);
            depart.setRepoId(repoId);
            list.add(depart);
        }

        this.saveBatch(list);
    }

    @Override
    public List<String> listAll(String repoId) {
        // 先删除
        QueryWrapper<RepoDepart> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RepoDepart::getRepoId, repoId);
        List<RepoDepart> list = this.list(wrapper);
        List<String> ids = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            for(RepoDepart item: list){
                ids.add(item.getDeptCode());
            }
        }

        return ids;
    }
}
